﻿var MessagesFactory = function() {
    var Elements = {
        toastImpactArea: $("#toastContainer"),
        toast: $(".toast"),
        toastTitle: $(".toast .toast-title"),
        toastIcon: $(".toast .toast-icon"),
        toastContent: $(".toast .toast-content"),
        dialog: $(".defaultDialog")
    };

    var ToastTypes = {
        Success: 0,
        Info: 1,
        Warning: 2
    };

    var initialized = false;
    if (!Elements.dialog.length) {
        $("body").append('<div class="defaultDialog dialog"></div>');
        Elements.dialog = $(".defaultDialog");
    }

    Elements.dialog.dialog({
        'templateId': 'defaultDialogTemplate'
    });

    var toastTimeoutID = null;

    var initialize = function() {
        if (initialized)
            return;
        Elements.toastImpactArea.bind('click', function(e) {
            hideToast();
            return Event.stopEvent(e);
        });
        initialized = true;
    };

    var showToast = function(title, message, type, timeout) {
        if (!initialized)
            initialize();
        Elements.toastIcon.removeClass();
        Elements.toastIcon.addClass("toast-icon");

        if (type == ToastTypes.Success)
            Elements.toastIcon.addClass("icon-success");
        else if (type == ToastTypes.Info)
            Elements.toastIcon.addClass("icon-info");
        else if (type == ToastTypes.Warning)
            Elements.toastIcon.addClass("icon-warning");

        Elements.toastTitle.html(title);
        Elements.toastContent.html(message);
        clearTimeout(toastTimeoutID);
        if (isNaN(timeout))
            timeout = Constants.notificationTimeVisible;
        if (timeout > 0)
            toastTimeoutID = setTimeout(function() { hideToast(); }, timeout);
        Elements.toastImpactArea.show();
    };

    var hideToast = function() {
        clearTimeout(toastTimeoutID);
        Elements.toastImpactArea.hide();
    };

    return {
        showToastSuccess: function(title, message, timeout) {
            showToast(title, message, ToastTypes.Success, timeout);
        },
        showToastInfo: function(title, message, timeout) {
            showToast(title, message, ToastTypes.Info, timeout);
        },
        showToastWarning: function(title, message, timeout) {
            showToast(title, message, ToastTypes.Warning, timeout);
        },
        showDialog: function(title, message, buttons) {
            Elements.dialog.dialog('show', {
                Title: title,
                Message: message,
                Buttons: buttons
            });
        },
        isDialogVisible: function() {
            return Elements.dialog.is(":visible");
        },
        hideToast: function() {
            hideToast();
        },
        hideDialog: function() {
            Elements.dialog.dialog('hide');
        },
        showError: function(message, callback) {
            if (!message)
                message = translate('General_EroareServer');
            var self = this;
            this.showDialog('eroare', message, [
                {
                    Text: 'OK',
                    Callback: function() {
                        self.hideDialog();
                        if (callback)
                            callback();
                    }
                }
            ]);
        }
    };
};